<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">
	var publicArchiveDatagrid;
	var publicArchiveTypeTree;
	$(function() {
		publicArchiveDatagrid = $('#archive_publicArchiveList_datagrid').datagrid({
			url : '${pageContext.request.contextPath}/publicArchiveAction!queryPublicArchive.action',
			fit : true,
			fitColumns : true,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 10,
			pageList : [ 10, 20 ],
			sortName : 'archiveName',
			sortOrder : 'asc',
			checkOnSelect : true,
			selectOnCheck : true,
			singleSelect : true,
			striped : true,
			columns : [ [ {
				field : 'id',
				title : '编号',
				width : 80,
				checkbox : true
			}, {
				field : 'archiveName',
				title : '文档名称',
				width : 180,
				align : 'left',
				sortable : true,
				formatter : function(value, row, index) {
					return '<span title="'+value+'">' + value + '</span>';
				}
			}, {
				field : 'keyword',
				title : '关键字',
				width : 150,
				align : 'left',
				formatter : function(value, row, index) {
					return '<span title="'+value+'">' + value + '</span>';
				}
			}, {
				field : 'uploaderId',
				title : '上传者',
				width : 100,
				align : 'left',
				hidden : true
			}, {
				field : 'uploaderName',
				title : '上传者',
				width : 60,
				align : 'left',
				sortable : true
			}, {
				field : 'uploadTime',
				title : '上传时间',
				width : 80,
				align : 'left',
				sortable : true
			}, {
				field : 'memo',
				title : '备注',
				width : 100,
				align : 'left',
				formatter : function(value, row, index) {
					return '<span title="'+value+'">' + value + '</span>';
				}
			} ] ],
			toolbar : [ {
				text : '查询',
				iconCls : 'icon-search',
				handler : function() {
					queryPublicArchive();
				}
			}, '-', {
				text : '新增',
				iconCls : 'icon-add',
				handler : function() {
					addPublicArchive();
				}
			}, '-', {
				text : '删除',
				iconCls : 'icon-remove',
				handler : function() {
					removePublicArchive();
				}
			}, '-', {
				text : '下载',
				iconCls : 'icon-down',
				handler : function() {
					downloadPublicArchive();
				}
			} ]
		});
		
		$('.datagrid-header div').css({
			'textAlign' : 'center'
		});

		function queryPublicArchive() {
			$('#archive_publicArchiveList_queryForm input').val('');
			$('#archive_publicArchiveList_queryDialog').dialog('open');
			var selectNode = publicArchiveTypeTree.tree('getSelected');
			if (selectNode != null) {
				$('#archive_publicArchiveList_queryForm input[name=archiveType]').val(selectNode.id);
			} else {
				$.messager.alert('提示', '请选择文档类型！');
			}
		}

		function addPublicArchive() {
			$('#archive_publicArchiveAdd_addPublicArchiveForm input').val('');
			var selectNode = publicArchiveTypeTree.tree('getSelected');
			if (selectNode != null) {
				var dlg = $('<div style="padding:20px 20px;" />').dialog({
					width : 480,
					height : 300,
					href : '${pageContext.request.contextPath}/pages/archive/publicArchiveAdd.jsp',
					modal : true,
					title : '文档新增',
					buttons : [ {
						text : '保存',
						handler : function() {
							$('#archive_publicArchiveAdd_addPublicArchiveForm').form('submit', {
								url : '${pageContext.request.contextPath}/publicArchiveAction!addPublicArchive.action',
								success : function(r) {
									var obj = jQuery.parseJSON(r);
									if (obj.success) {
										publicArchiveDatagrid.datagrid('insertRow', {
											index : 0,
											row : obj.obj
										});
										dlg.dialog('close');
										$.messager.show({
											title : '提示',
											msg : obj.msg
										});
									} else {
										$.messager.alert('提示', obj.msg);
									}
								}
							});
						}
					}, {
						text : '取消',
						handler : function() {
							dlg.dialog('close');
						}
					} ],
					onClose : function() {
						$(this).dialog('destroy');
					},
					onLoad : function() {
						$('#archive_publicArchiveAdd_addPublicArchiveForm input[name=archiveTypeName]').val(selectNode.text);
						$('#archive_publicArchiveAdd_addPublicArchiveForm input[name=archiveType]').val(selectNode.id);
					}
				});
			} else {
				$.messager.alert('提示', '请选择文档类型！');
			}
		}

		function removePublicArchive() {
			var rows = publicArchiveDatagrid.datagrid('getChecked');
			var ids = [];
			if (rows.length > 0) {
				$.messager.confirm('确认', '确定要删除选择的数据吗？', function(r) {
					if (r) {
						for ( var i = 0; i < rows.length; i++) {
							ids.push(rows[i].id);
						}
						$.ajax({
							url : '${pageContext.request.contextPath}/publicArchiveAction!removePublicArchive.action',
							data : {
								ids : ids.join(',')
							},
							dataType : 'json',
							success : function(r) {
								publicArchiveDatagrid.datagrid('load');
								publicArchiveDatagrid.datagrid('unselectAll');
								$.messager.show({
									title : '提示',
									msg : r.msg,
								});
							}
						});
					}
				});
			} else {
				$.messager.alert('提示', '请选择要删除的记录！');
			}
		}
		
		function downloadPublicArchive() {
			var rows = publicArchiveDatagrid.datagrid('getChecked');
			if (rows.length > 0) {
				$.messager.confirm('确认', '确定要下载选择的文档吗？', function(r) {
					if (r) {
						var id = rows[0].id;
						window.location.href = '${pageContext.request.contextPath}/publicArchiveAction!downloadPublicArchive.action?id=' + id;
					}
				});
			} else {
				$.messager.alert('提示', '请选择要下载的文档！');
			}
		}

		$('#archive_publicArchiveList_panel').panel({
			title : '文档分类',
			fit : true,
			border : false
			/*,
			tools : [ {
				iconCls : 'icon-reload',
				handler : function() {
					freshPublicArchiveType();
				}
			} ]*/
		});

		publicArchiveTypeTree = $('#archive_publicArchiveList_tree').tree({
			url : '${pageContext.request.contextPath}/treeAction!queryAllTreeNode.action?type=02',
			parentField : 'pid',
			lines : true,
			onDblClick : function(node) {
				var flag = publicArchiveTypeTree.tree('isLeaf', node.target);
				if (flag) {
					publicArchiveDatagrid.datagrid('load', {
						archiveType : node.id
					});
				}
			}
		});
		
		function freshPublicArchiveType() {
			publicArchiveTypeTree.tree({
				url : '${pageContext.request.contextPath}/treeAction!queryAllTreeNode.action?type=02',
				parentField : 'pid'
			});
		}

	});
</script>
<div id="archive_publicArchiveList_layout" class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'west',border:false" style="width:200px; border-right-width: 1px;">
		<div id="archive_publicArchiveList_panel" style="padding:5px 5px 0 5px">
			<ul id="archive_publicArchiveList_tree"></ul>
		</div>
	</div>
	<div data-options="region:'center',border:false">
		<table id="archive_publicArchiveList_datagrid"></table>
	</div>
</div>
<div id="archive_publicArchiveList_queryDialog" class="easyui-dialog" data-options="title:'文档查询',closed:true,modal:true,
			buttons:[{
				text : '查询',
				handler : function(){
					$('#archive_publicArchiveList_datagrid').datagrid('load', serializeObject($('#archive_publicArchiveList_queryForm')));
				    $('#archive_publicArchiveList_queryDialog').dialog('close');
				}
			},{
				text : '清空',
				handler : function(){
					$('#archive_publicArchiveList_queryForm input').val('');
				}
			}]" style="width:420px;height:180px;padding:20px 20px;" align="center">
	<form id="archive_publicArchiveList_queryForm" method="post">
		<table>
			<tr>
				<td style="text-align: right;">文档名称：</td>
				<td><input name="archiveName" style="width:240px;" />
				</td>
			</tr>
			<tr>
				<td style="text-align: right;">关键字：</td>
				<td><input name="keyword" style="width:240px;" />
				</td>
			</tr>
		</table>
		<input name="archiveType" type="hidden" />
	</form>
</div>